﻿#include "lua_obj.h"

static int lua_lv_table_set_cell_value(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row = luaL_checkinteger(L, 2);
    uint16_t col = luaL_checkinteger(L, 3);

    return 0;
}

static int lua_lv_table_set_cell_value_fmt(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row = luaL_checkinteger(L, 2);
    uint16_t col = luaL_checkinteger(L, 3);

    return 0;
}

static int lua_lv_table_set_row_cnt(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row_cnt = luaL_checkinteger(L, 2);
    lv_table_set_row_cnt(a, row_cnt);

    return 0;
}

static int lua_lv_table_set_col_cnt(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t col_cnt = luaL_checkinteger(L, 2);
    lv_table_set_col_cnt(a, col_cnt);

    return 0;
}

static int lua_lv_table_set_col_width(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t col_id = luaL_checkinteger(L, 2);

    return 0;
}

static int lua_lv_table_add_cell_ctrl(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row = luaL_checkinteger(L, 2);
    uint16_t col = luaL_checkinteger(L, 3);

    return 0;
}

static int lua_lv_table_clear_cell_ctrl(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row = luaL_checkinteger(L, 2);
    uint16_t col = luaL_checkinteger(L, 3);

    return 0;
}

static int lua_lv_table_get_cell_value(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row = luaL_checkinteger(L, 2);
    uint16_t col = luaL_checkinteger(L, 3);
    lv_table_get_cell_value(a, row, col);

    return 0;
}

static int lua_lv_table_get_row_cnt(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t res = lv_table_get_row_cnt(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_table_get_col_cnt(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t res = lv_table_get_col_cnt(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_table_get_col_width(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t col = luaL_checkinteger(L, 2);
    lv_table_get_col_width(a, col);

    return 0;
}

static int lua_lv_table_has_cell_ctrl(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    uint16_t row = luaL_checkinteger(L, 2);
    uint16_t col = luaL_checkinteger(L, 3);

    return 0;
}

static int lua_lv_table_get_selected_cell(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);


    return 0;
}

static int lua_auto_gc(lua_State *L)
{
    lv_obj_t *lv_obj = *(lv_obj_t **)luaL_checkudata(L, 1, META_TABLE);
    luaL_argcheck(L, lv_obj != NULL, 1, "invalid user data");
    if (lv_obj)
    {
        lv_obj_del(lv_obj);
    }
    return 0;
}
static const struct luaL_Reg arraylib_m[] = {
    {"set_cell_value", lua_lv_table_set_cell_value},
    {"set_cell_value_fmt", lua_lv_table_set_cell_value_fmt},
    {"set_row_cnt", lua_lv_table_set_row_cnt},
    {"set_col_cnt", lua_lv_table_set_col_cnt},
    {"set_col_width", lua_lv_table_set_col_width},
    {"add_cell_ctrl", lua_lv_table_add_cell_ctrl},
    {"clear_cell_ctrl", lua_lv_table_clear_cell_ctrl},
    {"get_cell_value", lua_lv_table_get_cell_value},
    {"get_row_cnt", lua_lv_table_get_row_cnt},
    {"get_col_cnt", lua_lv_table_get_col_cnt},
    {"get_col_width", lua_lv_table_get_col_width},
    {"has_cell_ctrl", lua_lv_table_has_cell_ctrl},
    {"get_selected_cell", lua_lv_table_get_selected_cell},
    LUA_LV_OBJ_RLT,
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(arraylib_m) / sizeof(struct luaL_Reg);
    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\n", arraylib_m[i].name);
    }
    return 1;
}

static const struct luaL_Reg arraylib_f[] = {
    {"help", help},
    {NULL, NULL}};

LUALIB_API int lua_lv_table_funinit(lua_State *L)
{
    luaL_newlib(L, arraylib_f);
    luaL_newmetatable(L, META_TABLE);  /* create metatable for file handles */
    lua_pushvalue(L, -1);            /* push metatable */
    lua_setfield(L, -2, "__index");  /* metatable.__index = metatable */
    luaL_setfuncs(L, arraylib_m, 0); /* add file methods to new metatable */
    lua_pop(L, 1);                   /* pop new metatable */

    return 1;
}
